package com.ccrfid.rmc6.service.sys;

import com.ccrfid.rmc6.entity.sys.AppModuleEntity;
import com.ccrfid.rmc6.entity.sys.RoleModuleRelation;
import com.ccrfid.rmc6.pojo.dto.sys.AppModuleDto;
import com.ccrfid.rmc6.repository.sys.AppModuleRepository;
import com.ccrfid.rmc6.repository.sys.RoleModuleRelationRepository;
import com.ccrfid.rmc6.resource.auth.pojo.UserPrincipal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@Transactional
public class AppModuleServiceImpl implements AppModuleService {
    private static final Logger logger = LoggerFactory.getLogger(AppModuleServiceImpl.class);

    @Autowired
    private RoleModuleRelationRepository roleModuleRelationRepository;
    @Autowired
    private AppModuleRepository appModuleRepository;

    @Override
    public List<AppModuleEntity> getAppModules(UserPrincipal userPrincipal) {
        List<Long> roleIdList = userPrincipal.getRoles();
        if (roleIdList != null && roleIdList.size() > 0) {
            List<RoleModuleRelation> roleModuleRelationList = roleModuleRelationRepository.getAllByRoleIdIn(roleIdList);
            if (roleModuleRelationList != null && roleModuleRelationList.size() > 0) {
                Set<Long> moduleIdSet = new HashSet<>();
                for (RoleModuleRelation relation : roleModuleRelationList) {
                    moduleIdSet.add(relation.getModuleId());
                }
                return appModuleRepository.getAllByEnabledAndIdInOrderBySortNumber(1, moduleIdSet);
            }
        }
        return new ArrayList<>();
    }

    @Override
    public List<AppModuleDto> getAppModules2(UserPrincipal userPrincipal) {
        List<AppModuleDto> list = new ArrayList<>();
        List<Long> roleIdList = userPrincipal.getRoles();
        if (roleIdList != null && roleIdList.size() > 0) {
            List<RoleModuleRelation> roleModuleRelationList = roleModuleRelationRepository.getAllByRoleIdIn(roleIdList);
            if (roleModuleRelationList != null && roleModuleRelationList.size() > 0) {
                Set<Long> moduleIdSet = new HashSet<>();
                for (RoleModuleRelation relation : roleModuleRelationList) {
                    moduleIdSet.add(relation.getModuleId());
                }
                List<AppModuleEntity> moduleEntityList =
                        appModuleRepository.getAllByEnabledAndIdInAndPidIsNullOrderBySortNumber(1, moduleIdSet);
                if (moduleEntityList != null && moduleEntityList.size() > 0) {
                    for (AppModuleEntity moduleEntity : moduleEntityList) {
                        list.add(getAppModuleDto(moduleEntity, moduleIdSet));
                    }
                }
            }
        }
        return list;
    }

    private AppModuleDto getAppModuleDto(AppModuleEntity appModuleEntity, Set<Long> moduleIdSet) {
        AppModuleDto dto = new AppModuleDto();
        dto.setModule(appModuleEntity);
        List<AppModuleDto> subModuleDtoList = new ArrayList<>();
        List<AppModuleEntity> moduleEntityList =
                appModuleRepository.getAllByEnabledAndIdInAndPidOrderBySortNumber(1, moduleIdSet, appModuleEntity.getId());
        if (moduleEntityList != null && moduleEntityList.size() > 0) {
            for (AppModuleEntity moduleEntity : moduleEntityList) {
                subModuleDtoList.add(getAppModuleDto(moduleEntity, moduleIdSet));
            }
        }
        dto.setSubModules(subModuleDtoList);
        return dto;
    }
}
